package com.ymatou.iapi.settlement.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.collections.CollectionUtils;
import org.apache.http.client.ClientProtocolException;

import com.ymatou.iapi.accounting.parameter.AccountBean;
import com.ymatou.iapi.accounting.parameter.GetAccountListByUserIdBean;
import com.ymatou.iapi.accounting.service.GetAccountListByUserIdCall;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.parameter.vo.CouponVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderItemVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderVo;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.settlement.parameter.AdRateInfoPo;
import com.ymatou.iapi.settlement.parameter.AdRateProductInfo;
import com.ymatou.iapi.settlement.parameter.GenerateAdRateInfoReq;
import com.ymatou.iapi.settlement.parameter.GenerateAnliSettlementReq;
import com.ymatou.iapi.settlement.parameter.GenerateRefundCommissionInvoiceBean;
import com.ymatou.iapi.settlement.parameter.SettleAdCostReq;
import com.ymatou.iapi.settlement.parameter.SettleSellerIncomeAndChargeCommissionBean;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.YMTTrading.TradingAllPromotion;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.business.service.YMTTrading.TradingProductVo;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sqlwapper.YmtSettlementInvoiceWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

@SuppressWarnings("static-access")
public class SettlementService {

	// 有佣金费率的 一级类目
	public static final int firstCataid = 1001;
	// 有佣金费率的 二级类目
	public static final int secondCategoryId = 1052;
	
	private static Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
	private static String sellerName = String.valueOf(EnvSetup.getData("autotest_sellerName1"));

	private static GenerateAdRateInfoReq generateAdRateInfoReq = new GenerateAdRateInfoReq();
	private static GenerateAdRateInfoCall generateAdRateInfoCall = new GenerateAdRateInfoCall();

	/**
	 * 
	 * 一次下单同一个商家1个订单,订单下面2个商品 不使用优惠券
	 * 
	 * @throws IOException
	 * @throws ClientProtocolException
	 * 
	 */
	public static TradingServiceResp placeOrder(int sellerId)
			throws ClientProtocolException, IOException {

		String sellerName = new JUserCallService().GetUserInfo(sellerId)
				.getLoginId();
		
		PlaceOrderReq placeorderBean = TradingCallService
				.buildStandPlaceOrderReq();
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		// 创建一个普通商品
		TradingProductVo product = TradingProductVo.createPro((long)sellerId,
				sellerName, 100);
		
		OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
				product.getProductId(), product.getCatalogId());		
		
		orderVo.setOrderItems(Arrays.asList(orderItemVo));
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		placeorderBean.setOrderGroups(Arrays.asList(orderGroupVo));

		return TradingCallService.callPlaceOrderService(placeorderBean);
	}

	/**
	 * 
	 * 一次下单同一个商家1个订单分别使用平台,商家优惠券,多个商品 固定 一级类目 带佣金费率
	 * 
	 * @param isFreightFree
	 *            是否包邮
	 * @throws IOException
	 * @throws ClientProtocolException
	 * 
	 */
	public static TradingServiceResp placeOrder2(boolean isFreightFree)
			throws ClientProtocolException, IOException {
	
		Long sellerId = Long.valueOf(EnvSetup.getData("SellerId"));
		String sellerName = new JUserCallService().GetUserInfo(sellerId.intValue())
				.getLoginId();

		PlaceOrderReq placeorderBean = TradingCallService
				.buildStandPlaceOrderReq();
		
		Long userId = placeorderBean.getUserId();
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		// 创建一个普通商品
		TradingProductVo product1 = TradingProductVo.createPro(sellerId,
				sellerName, 100);
		TradingProductVo product2 = TradingProductVo.createPro(sellerId,
				sellerName, 100);
		
		OrderItemVo orderItemVo1 = TradingCallService.createOrderItemVo(
				product1.getProductId(), product1.getCatalogId());
		OrderItemVo orderItemVo2 = TradingCallService.createOrderItemVo(
				product2.getProductId(), product2.getCatalogId());
		
		// 第二个商品价格设置一个大金额，佣金收费超过最大150阀值
		orderItemVo2.setProductPrice(new BigDecimal(3100));
		orderItemVo2.setOriginalPrice(new BigDecimal(3100));
		
		orderVo.setOrderItems(Arrays.asList(orderItemVo1, orderItemVo2));
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		placeorderBean.setOrderGroups(Arrays.asList(orderGroupVo));

		placeorderBean.setOrderGroups(Arrays.asList(TradingCallService
				.createOrderGroupVo(sellerId, sellerName)));
		
		CouponVo sellerCoupon1 = TradingAllPromotion.createSellerCouponVo(
				(long) userId, (long) sellerId, new BigDecimal(30),
				BigDecimal.TEN);

		placeorderBean.getOrderGroups().get(0).setSellerCoupon(sellerCoupon1);

		CouponVo ymatouCoupon = TradingAllPromotion.createYMTCouponVo(
				(long) userId, new BigDecimal(60), BigDecimal.TEN);
		placeorderBean.setYmatouCoupon(ymatouCoupon);

		// 设置没有商品的随机运费
		for (int i = 0; i < 1; i++) {

			for (int j = 0; j < 1; j++) {

				placeorderBean
						.getOrderGroups()
						.get(i)
						.getOrders()
						.get(j)
						.getOrderItems()
						.forEach(
								item -> {

									if (!isFreightFree) {

										item.setFreight(new BigDecimal(
												new Random().nextInt(50)));
									}

									item.setFirstCategoryId(firstCataid);									
								});
			}
		}

		return TradingCallService.callPlaceOrderService(placeorderBean);
	}

	/**
	 * 
	 * 一次下单同一个商家1个订单分别使用平台,商家优惠券,多个商品 固定 二级类目 带佣金费率
	 * 
	 * @param isFreightFree
	 *            是否包邮
	 * @throws IOException
	 * @throws ClientProtocolException
	 * 
	 * 
	 */
	public static TradingServiceResp placeOrder3(boolean isFreightFree)
			throws ClientProtocolException, IOException {

		Long sellerId = Long.valueOf(EnvSetup.getData("SellerId"));
		String sellerName = new JUserCallService().GetUserInfo(sellerId.intValue())
				.getLoginId();

		PlaceOrderReq placeorderBean = TradingCallService
				.buildStandPlaceOrderReq();
		
		Long userId = placeorderBean.getUserId();
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		// 创建一个普通商品
		TradingProductVo product1 = TradingProductVo.createPro(sellerId,
				sellerName, 100);
		TradingProductVo product2 = TradingProductVo.createPro(sellerId,
				sellerName, 100);
		
		OrderItemVo orderItemVo1 = TradingCallService.createOrderItemVo(
				product1.getProductId(), product1.getCatalogId());
		OrderItemVo orderItemVo2 = TradingCallService.createOrderItemVo(
				product2.getProductId(), product2.getCatalogId());
		
		// 第二个商品价格设置一个大金额，佣金收费超过最大150阀值
		orderItemVo2.setProductPrice(new BigDecimal(3100));
		orderItemVo2.setOriginalPrice(new BigDecimal(3100));
		
		orderVo.setOrderItems(Arrays.asList(orderItemVo1, orderItemVo2));
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		placeorderBean.setOrderGroups(Arrays.asList(orderGroupVo));

		placeorderBean.setOrderGroups(Arrays.asList(TradingCallService
				.createOrderGroupVo(sellerId, sellerName)));
		
		CouponVo sellerCoupon1 = TradingAllPromotion.createSellerCouponVo(
				(long) userId, (long) sellerId, new BigDecimal(30),
				BigDecimal.TEN);

		placeorderBean.getOrderGroups().get(0).setSellerCoupon(sellerCoupon1);

		CouponVo ymatouCoupon = TradingAllPromotion.createYMTCouponVo(
				(long) userId, new BigDecimal(60), BigDecimal.TEN);
		placeorderBean.setYmatouCoupon(ymatouCoupon);

		// 设置没有商品的随机运费
		for (int i = 0; i < 1; i++) {

			for (int j = 0; j < 1; j++) {

				placeorderBean
						.getOrderGroups()
						.get(i)
						.getOrders()
						.get(j)
						.getOrderItems()
						.forEach(
								item -> {

									if (!isFreightFree) {

										item.setFreight(new BigDecimal(
												new Random().nextInt(50)));
									}

									item.setFirstCategoryId(firstCataid);
									item.setSecondCategoryId(secondCategoryId);

								});

			}

		}

		return TradingCallService.callPlaceOrderService(placeorderBean);
	}

	/**
	 * 新交易下单 1个买手,多商品,生成2个订单
	 * 
	 * @param isUserCoupon
	 *            是否使用优惠券（同时使用平台券，商家券）
	 * @return
	 */
	public static TradingServiceResp placeOrder4(boolean isUserCoupon) {

		PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();

		long userId = placeOrderReq.getUserId();		
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		
		// 买手第一个订单
		OrderVo orderVo1 = TradingCallService.createOrderVo();

		TradingProductVo productVo1 = TradingProductVo.createPro(sellerId, sellerName, 10);
		TradingProductVo productVo2 = TradingProductVo.createPro(sellerId, sellerName, 10);	

		OrderItemVo orderItemVo1 = TradingCallService.createOrderItemVo(
				productVo1.getProductId(), productVo1.getCatalogId());
		OrderItemVo orderItemVo2 = TradingCallService.createOrderItemVo(
				productVo2.getProductId(), productVo2.getCatalogId());
		
		orderVo1.setOrderItems(Arrays.asList(orderItemVo1, orderItemVo2));
		
		// 买手第二个订单
		OrderVo orderVo2 = TradingCallService.createOrderVo();
		TradingProductVo productVo3 = TradingProductVo.createPro(sellerId, sellerName, 10);
		OrderItemVo orderItemVo3 = TradingCallService.createOrderItemVo(
				productVo3.getProductId(), productVo3.getCatalogId());
		
		orderVo2.setOrderItems(Arrays.asList(orderItemVo3));

		orderGroupVo.setOrders(Arrays.asList(orderVo1, orderVo2));
		// FirstCategoryId(1149) 设置为1149 类目 有收佣的费率
		orderGroupVo.getOrders().forEach(
				order -> order.getOrderItems().forEach(
						item -> item.setFirstCategoryId(firstCataid)));

		if (isUserCoupon) {

			CouponVo sellerCoupon1 = TradingAllPromotion.createSellerCouponVo(
					userId, sellerId, new BigDecimal(30), BigDecimal.TEN);

			orderGroupVo.setSellerCoupon(sellerCoupon1);

			CouponVo ymatouCoupon = TradingAllPromotion.createYMTCouponVo(
					userId, new BigDecimal(60), new BigDecimal("5.00"));
			placeOrderReq.setYmatouCoupon(ymatouCoupon);
		}
		
		placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

		return TradingCallService.callPlaceOrderService(placeOrderReq);
	}
	
	public static TradingServiceResp placeAnlihuiOrderExcludeFreight(List<BigDecimal> rebateRatios) {

		PlaceOrderReq placeorderBean = TradingCallService
				.buildStandPlaceOrderReq();
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		// 创建一个普通商品
		TradingProductVo product = TradingProductVo.createPro(sellerId,
				sellerName, 100);
		
		OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
				product.getProductId(), product.getCatalogId());
		
		orderItemVo.setFreight(BigDecimal.ZERO);
		orderItemVo.setSellerRebateRatio(rebateRatios.get(0));
		orderItemVo.setRebateRatio(rebateRatios.get(1));
		orderItemVo.setAnliPromotionRuleId("autotestAnliPromotionRuleId");
		
		orderVo.setOrderType(0);
		orderVo.setOrderItems(Arrays.asList(orderItemVo));
		
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		placeorderBean.setOrderGroups(Arrays.asList(orderGroupVo));	
		
		return TradingCallService.callPlaceOrderService(placeorderBean);
	}

	/***
	 * 
	 * 生成一个订单多个商品广告费数据
	 * 
	 * @param sellerId
	 * @param isMultiple
	 *            是否创建多个广告商品费用数据
	 * 
	 */
	public static List<AdRateInfoPo> generateAdRateInfo(int sellerId,
			boolean isMultiple) {

		List<AdRateInfoPo> adRateInfoPoList = new ArrayList<AdRateInfoPo>();

		try {

			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			long userId = placeOrderResp.getUserId();
			long traidngId = placeOrderResp.getTradingId();
			BigDecimal payAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, payAmount, traidngId);

			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();

			List<AdRateProductInfo> adRateProductInfoList = new ArrayList<AdRateProductInfo>();

			for (OrderDetailPo orderInfo : orderInfoList) {

				AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

				BigDecimal adCostMax = new BigDecimal(
						1 + new Random().nextInt(30));

				adRateProductInfo.setProductId(orderInfo.getProductId());

				adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

				adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
						.toString());

				adRateProductInfo.setAdRate(new BigDecimal(new Random()
						.nextFloat() / 10));

				adRateProductInfo.setAdCostMax(adCostMax);

				adRateProductInfoList.add(adRateProductInfo);

			}

			// 不是多商品广告
			if (!isMultiple)
				adRateProductInfoList.remove(0);

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(adRateProductInfoList);

			generateAdRateInfoCall.setData(generateAdRateInfoReq);

			generateAdRateInfoCall.callService();

			adRateInfoPoList = new YmtSettlementInvoiceWapper()

			.selectYmtAdRateInfo(orderId);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return adRateInfoPoList;

	}

	/***
	 * 
	 * 生成一个订单多个商品广告费数据
	 * 
	 * @param sellerId
	 * @param isMultiple
	 *            是否创建多个广告商品费用数据
	 * 
	 */
	public static List<AdRateInfoPo> generateAdRateInfo(
			TradingServiceResp placeOrderResp, BigDecimal adRate) {

		List<AdRateInfoPo> adRateInfoPoList = new ArrayList<AdRateInfoPo>();

		try {

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			long userId = placeOrderResp.getUserId();
			long traidngId = placeOrderResp.getTradingId();
			BigDecimal payAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, payAmount, traidngId);

			List<OrderDetailPo> orderInfoList = order.getOrderDetailPoList();

			List<AdRateProductInfo> adRateProductInfoList = new ArrayList<AdRateProductInfo>();

			for (OrderDetailPo orderInfo : orderInfoList) {

				AdRateProductInfo adRateProductInfo = new AdRateProductInfo();

				BigDecimal adCostMax = new BigDecimal(100);

				adRateProductInfo.setProductId(orderInfo.getProductId());

				adRateProductInfo.setAdId(TestDataManager.getRandomIntNum(9));

				adRateProductInfo.setAdCostId(TestDataManager.getRandomUUID()
						.toString());

				adRateProductInfo.setAdRate(adRate);

				adRateProductInfo.setAdCostMax(adCostMax);

				adRateProductInfoList.add(adRateProductInfo);

			}

			generateAdRateInfoReq.setOrderId(orderId);

			generateAdRateInfoReq.setAdProductInfos(adRateProductInfoList);

			generateAdRateInfoCall.setData(generateAdRateInfoReq);

			generateAdRateInfoCall.callService();

			adRateInfoPoList = new YmtSettlementInvoiceWapper()
					.selectYmtAdRateInfo(orderId);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return adRateInfoPoList;

	}
	
	/***
	 * 
	 * 生成一个订单多个商品广告费数据
	 * 
	 * @param orderId
	 * @param adRateProductInfoList
	 *            是否创建多个广告商品费用数据
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 * 
	 */
	public static void generateAdRateInfo(long orderId,
			List<AdRateProductInfo> adRateProductInfoList)
			throws ClientProtocolException, IOException {

		generateAdRateInfoReq.setOrderId(orderId);

		generateAdRateInfoReq.setAdProductInfos(adRateProductInfoList);

		generateAdRateInfoCall.setData(generateAdRateInfoReq);

		generateAdRateInfoCall.callService();
	}

	/***
	 * 
	 * 根据用户id 查询账户信息
	 * 
	 * @param userId
	 * 
	 * 
	 */
	public static Map<Integer, AccountBean> getAccountByUserId(int userId) {

		Map<Integer, AccountBean> map = new HashMap<Integer, AccountBean>();

		try {
			GetAccountListByUserIdBean getaccountlistbyuseridBean = new GetAccountListByUserIdBean();
			GetAccountListByUserIdCall getaccountlistbyuseridCall = new GetAccountListByUserIdCall();
			getaccountlistbyuseridBean.setUserId(userId);
			getaccountlistbyuseridBean.setAppId("AutoTest");
			getaccountlistbyuseridCall.setData(false,
					getaccountlistbyuseridBean);
			getaccountlistbyuseridCall.callService();

			ArrayList<AccountBean> list = getaccountlistbyuseridCall
					.getReturnAccountList();

			if (!CollectionUtils.isEmpty(list)) {

				for (AccountBean account : list) {

					map.put(account.getAccountType(), account);

				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;

	}

	/**
	 * 
	 重置 买手广告费账户 13
	 * 
	 * @param buyerId
	 * @param newAmount
	 */

	public static void resetBuyerAdvertisingFeeAccount(int buyerId,
			BigDecimal newAmount) {

		try {

			String accountid = AccountCallServiceV2.getAccountId(buyerId, 13);
			AccountCallServiceV2.resetAccount(accountid, newAmount,
					BigDecimal.ZERO);
		} catch (Exception e) {
			// TODO: handle exception

			e.printStackTrace();
		}
	}

	/**
	 * 
	 重置 买手奖金池账户 14
	 * 
	 * @param buyerId
	 * @param newAmount
	 */
	public static void resetBuyerRewardAccount(int buyerId, BigDecimal newAmount) {

		try {
			String accountid = AccountCallServiceV2.getAccountId(buyerId, 14);

			AccountCallServiceV2.resetAccount(accountid, newAmount,
					new BigDecimal(0));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 
	 重置 买手余额账户 1
	 * 
	 * @param buyerId
	 * @param newAmount
	 */
	public static void resetRmbAccount(int buyerId, BigDecimal newAmount) {

		try {
			String accountid = AccountCallServiceV2.getAccountId(buyerId, 1);

			AccountCallServiceV2.resetAccount(accountid, newAmount,
					new BigDecimal(0));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 构造一个结算失败的订单 14
	 * 
	 * @return orderId
	 */
	public static long settleAdCostFail() {

		SettleAdCostReq settleAdCostReq = new SettleAdCostReq();
		SettleAdCostCall settleAdCostCall = new SettleAdCostCall();

		int sellerId = Integer.valueOf(EnvSetup.getData("BuyerId"));

		// 重置广告费用账户
		resetBuyerAdvertisingFeeAccount(sellerId, BigDecimal.ONE);
		// 重置广告奖金账户
		resetBuyerRewardAccount(sellerId, new BigDecimal(2));
		// 重置余额奖金账户
		resetRmbAccount(sellerId, BigDecimal.ZERO);

		List<AdRateInfoPo> adRateInfoPoList = generateAdRateInfo(sellerId, true);

		long orderId = adRateInfoPoList.get(0).getOrderId();

		settleAdCostReq.setOrderId(orderId);

		settleAdCostCall.setData(false, settleAdCostReq);

		try {
			settleAdCostCall.callService();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return orderId;

	}

	/**
	 * 计算广告费
	 * 
	 * @param orderId
	 */
	public static void settleAdCost(int orderId) {

		SettleAdCostReq settleAdCostReq = new SettleAdCostReq();
		SettleAdCostCall settleAdCostCall = new SettleAdCostCall();

		settleAdCostReq.setOrderId(orderId);

		settleAdCostCall.setData(false, settleAdCostReq);

		try {
			settleAdCostCall.callService();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 确认收货后,结算收入和扣佣
	 * 
	 * @param orderId
	 */
	public static void settlesellerIncome(long orderId) {

		SettleSellerIncomeAndChargeCommissionBean settlesellerincomeandchargecommissionBean = new SettleSellerIncomeAndChargeCommissionBean();
		SettleSellerIncomeAndChargeCommissionCall settlesellerincomeandchargecommissionCall = new SettleSellerIncomeAndChargeCommissionCall();
		settlesellerincomeandchargecommissionBean.setOrderId(orderId);
		settlesellerincomeandchargecommissionCall.setData(false,
				settlesellerincomeandchargecommissionBean);
		try {
			settlesellerincomeandchargecommissionCall.callService();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/***
	 * 根据商家id,生成商品广告费用结算数据
	 */
	private static void generateSettleAdCostTestData(int sellerId) {

		SettleAdCostReq settleAdCostReq = new SettleAdCostReq();
		SettleAdCostCall settleAdCostCall = new SettleAdCostCall();

		long orderId = generateAdRateInfo(sellerId, true).get(0).getOrderId();

		settleAdCostReq.setOrderId(orderId);

		settleAdCostCall.setData(false, settleAdCostReq);

		try {
			settleAdCostCall.callService();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void generateRefund(String refundBillNo) {

		GenerateRefundCommissionInvoiceBean generaterefundcommissioninvoiceBean = new GenerateRefundCommissionInvoiceBean();
		GenerateRefundCommissionInvoiceCall generaterefundcommissioninvoiceCall = new GenerateRefundCommissionInvoiceCall();

		GenerateRefundCommissionInvoiceBean.ParamExt paramExt = new GenerateRefundCommissionInvoiceBean.ParamExt();

		paramExt.setRefundBillNo(refundBillNo);
		generaterefundcommissioninvoiceBean.setParamExt(paramExt);
		generaterefundcommissioninvoiceCall.setData(false,
				generaterefundcommissioninvoiceBean);
		try {
			generaterefundcommissioninvoiceCall.callService();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	/**
	 * 商品支付金额
	 * @param orderDetailPo
	 * @return
	 */
	public static BigDecimal getOrderDetailPaidAmount(
			OrderDetailPo orderDetailPo) {
		
		if (Utils.falseIfNull(orderDetailPo.getFullReductionFreeFreight()))
			return Utils
					.zeroIfNull(orderDetailPo.getProductPrice())
					.multiply(new BigDecimal(orderDetailPo.getProductCount()))
					.add(Utils.zeroIfNull(orderDetailPo.getDiscount()))
					.subtract(Utils.zeroIfNull(orderDetailPo.getYmtCouponAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo.getSellerCouponAmount()))
					.subtract(Utils.zeroIfNull(orderDetailPo.getPromotionAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo.getYmtPromotionAmount()))
					.add(BigDecimal.ZERO);
		else
			return Utils
					.zeroIfNull(orderDetailPo.getProductPrice())
					.multiply(new BigDecimal(orderDetailPo.getProductCount()))
					.add(Utils.zeroIfNull(orderDetailPo.getDiscount()))
					.subtract(Utils.zeroIfNull(orderDetailPo.getYmtCouponAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo.getSellerCouponAmount()))
					.subtract(Utils.zeroIfNull(orderDetailPo.getPromotionAmount()))
					.subtract(
							Utils.zeroIfNull(orderDetailPo.getYmtPromotionAmount()))
					.add(Utils.zeroIfNull(orderDetailPo.getFreight()));
	}
	
	/**
	 * 商品支付金额
	 * @param orderDetailPo
	 * @return
	 */
	public static BigDecimal getOrderDetailPaidAmountExcludeFreight(
			OrderDetailPo orderDetailPo) {
		return Utils
				.zeroIfNull(orderDetailPo.getProductPrice())
				.multiply(new BigDecimal(orderDetailPo.getProductCount()))
				.add(Utils.zeroIfNull(orderDetailPo.getDiscount()))
				.subtract(
						Utils.zeroIfNull(orderDetailPo.getSellerCouponAmount()))
				.subtract(Utils.zeroIfNull(orderDetailPo.getPromotionAmount()));
	}
	
	/**
	 * 生成安利结算
	 * @param orderId
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static void generateAnliSettlement(long orderId) throws ClientProtocolException, IOException {
		
		GenerateAnliSettlementReq generateAnliSettlementReq = new GenerateAnliSettlementReq();
		GenerateAnliSettlementCall generateAnliSettlementCall = new GenerateAnliSettlementCall();
	
		generateAnliSettlementReq.setOrderId(orderId);
		
		generateAnliSettlementCall.setData(generateAnliSettlementReq);
		
		generateAnliSettlementCall.callService();
	}

	public static void main(String args[]) {

		// SettlementService.generateAdRateInfo(20123134, false);

		for (int i = 0; i < 10; i++) {
			SettlementService.generateSettleAdCostTestData(3383);
		}

		/*
		 * SettlementService.resetBuyerAdvertisingFeeAccount(20123134, new
		 * BigDecimal(100000));
		 */

		SettlementService.settleAdCost(112531257);

	}
}
